package com.kmdd.xtds.util

import android.os.Build
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by AD on 2016/5/1.
 * Edited by Fred on 2016/11/8
 * 时间格式转换工具
 */
object TimeUtil {
    var talkingtime: Long = 0
    const val DATEFORMATER = "yyyy-MM-dd HH:mm:ss"
    const val DATEFORMATER2 = "yyyy-MM-dd"
    const val DATEFORMATER3 = "HH:mm:ss"
    const val DATEFORMATER4 = "MM-dd HH:mm"
    const val DATEFORMATER5 = "yyyy/MM/dd"
    const val DATEFORMATER6 = "HH:mm"
    const val DATEFORMATER7 = "yy/MM/dd"
    const val DATEFORMATER8 = "MM-dd HH:mm:ss"
    const val DATEFORMATER9 = "yyyy年MM月dd日"
    private val dateFormater: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER)
            }
        }
    private val dateFormater2: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER2)
            }
        }
    private val dateFormater3: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER3)
            }
        }
    private val dateFormater4: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER4)
            }
        }
    private val dateFormater5: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER5)
            }
        }
    private val dateFormaterHM: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER6)
            }
        }
    private val dateFormater8: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER8)
            }
        }
    private val dateFormater9: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue(): SimpleDateFormat {
                return SimpleDateFormat(DATEFORMATER9)
            }
        }

    fun getTime(timestamp: Long, type: String): String {
        val date = Date(timestamp)
        var dateTime: String = ""
        when (type) {
            DATEFORMATER -> dateTime = dateFormater.get().format(date)
            DATEFORMATER2 -> dateTime = dateFormater2.get().format(date)
            DATEFORMATER3 -> dateTime = dateFormater3.get().format(date)
            DATEFORMATER4 -> dateTime = dateFormater4.get().format(date)
            DATEFORMATER5 -> dateTime = dateFormater5.get().format(date)
            DATEFORMATER6 -> dateTime = dateFormaterHM.get().format(date)
            DATEFORMATER7 -> {
                val format = dateFormater5.get().format(date)
                dateTime = format.substring(2, format.length)
            }
            DATEFORMATER9 -> dateTime = dateFormater9.get().format(date)
            else -> {}
        }
        return dateTime
    }

    fun getTimeMS(endTime: Long): String {
        val date = Date(endTime)
        val formatter = SimpleDateFormat("mm:ss")
        return formatter.format(date)
    }

    fun getTimeMZHS(endTime: Long): String {
        val date = Date(endTime)
        val formatter = SimpleDateFormat("mm分ss秒")
        return formatter.format(date)
    }

    fun getRuntime(endTime: Long): String {
        val date = Date(endTime)
        val formatter = SimpleDateFormat("mm:ss:SSS")
        var runTime = formatter.format(date)
        runTime = runTime.substring(0, runTime.length - 1)
        return runTime
    }

    fun getMediaDurtionTime(time: Long): String { //
        val hour = (time / 3600).toInt()
        var minute = (time % 3600).toInt()
        if (minute != 0) {
            minute = minute / 60
        }
        val second = (time % 60).toInt()
        var houstr: String? = null
        var minuteStr: String? = null
        var secondStr: String? = null
        houstr = if (hour < 10) {
            "0$hour"
        } else {
            hour.toString()
        }
        minuteStr = if (minute < 10) {
            "0$minute"
        } else {
            minute.toString()
        }
        secondStr = if (second < 10) {
            "0$second"
        } else {
            second.toString()
        }
        var endtime: String? = null
        /*if (houstr.equals("00")) {//只有时位为0才输出分位
            endtime = minuteStr + ":" + secondStr;
        } else {//默认最高位是时位
            endtime = houstr + ":" + minuteStr + ":" + secondStr;
        }*/endtime = "$houstr:$minuteStr:$secondStr"
        return endtime
    }

    //把时间转化为时间戳
    fun getTime(timeString: String?): Long {
        var timeStamp = 0L
        val sdf = SimpleDateFormat("HH:mm")
        val d: Date
        try {
            d = sdf.parse(timeString)
            timeStamp = d.time
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return timeStamp
    }

    //把环信时间转化为秒
    fun getEaseMobTime(timeString: String): Long {
        val time = timeString.split(":").toTypedArray()
        var timeStamp = 0L
        //最后一次取秒数
        timeStamp = java.lang.Long.valueOf(time[1])

        /*  SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        Date d;
        try {
            d = sdf.parse(timeString);
            timeStamp = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }*/return timeStamp
    }

    var result1 = ""

    //时间戳字符串转Long 时间戳 *1000变成毫秒
    fun getTimeStamp(timestamp: String): Long {
        return (timestamp.toDouble() * 1000L).toLong()
    }

    //时间戳字符串转Long 时间戳 *1000变成毫秒
    fun getTimeStamp(timestamp: Long): Long {
        return timestamp * 1000L
    }

    //由时间戳转成年月日 分时秒
    fun getDateYMDHMS(timestamp: Long): String {
        val date = Date(timestamp * 1000)
        return dateFormater.get().format(date)
    }

    fun getDateYMD(timestamp: Long): String {
        val date = Date(timestamp * 1000)
        return dateFormater2.get().format(date)
    }

    fun getDateYMD1(timestamp: Long): String {
        val date = Date(timestamp)
        return dateFormater2.get().format(date)
    }

    //由时间戳转成月日 分时
    fun getDateMDHM(timestamp: Long): String {
        val date = Date(timestamp)
        val dateTime = dateFormater4.get().format(date)
        val dateString = dateTime.split("-").toTypedArray()
        return dateString[0] + "月" + dateString[1]
            .substring(0, 2) + "日 " + dateString[1].substring(3)
    }

    //由时间戳转成月日 分时
    fun getDateMDHMS(timestamp: Long): String {
        val date = Date(timestamp)
        val dateTime = dateFormater8.get().format(date)
        val dateString = dateTime.split("-").toTypedArray()
        return dateString[0] + "月" + dateString[1]
            .substring(0, 2) + "日 " + dateString[1].substring(3)
    }

    //由时间戳转成分时秒
    fun getDgetDateHMS(timestamp: Long): String {
        val date = Date(timestamp)
        return dateFormater3.get().format(date)
    }

    //由时间戳转成分时
    fun getDgetDateHM(timestamp: Long): String {
        val date = Date(timestamp)
        return dateFormaterHM.get().format(date)
    }

    //由时间戳转成分时秒
    fun getDgetDateHMSZh(timestamp: Long): String {
        val date = Date(timestamp)
        val dateTime = dateFormater3.get().format(date)
        val dateString = dateTime.split(":").toTypedArray()
        return dateString[0] + "小时" + dateString[1] + "分 " + dateString[2] + "秒"
    }

    fun getDgetDateHMSZh(dateTime: String): String {
        val dateString = dateTime.split(":").toTypedArray()
        var dateLast = dateString[0] + "小时" + dateString[1] + "分 " + dateString[2] + "秒"
        if (Integer.valueOf(dateString[0]) > 24) {
            dateLast = (Integer.valueOf(dateString[0]) / 24).toString() + "天" + Integer.valueOf(
                dateString[0]
            ) % 24 + "小时" + dateString[1] + "分 " + dateString[2] + "秒"
        }
        return dateLast
    }

    //把时间戳转化为友好时间
    fun friendly_time(timestamp: Long): String {
        //获取time距离当前的秒数
        val ct = ((System.currentTimeMillis() - timestamp) / 1000).toInt()
        if (ct == 0) {
            return "刚刚"
        }
        if (ct > 0 && ct < 60) {
            return ct.toString() + "秒前"
        }
        if (ct >= 60 && ct < 3600) {
            return Math.max(ct / 60, 1).toString() + "分钟前"
        }
        if (ct >= 3600 && ct < 86400) return "${ct / 3600}小时前"
        if (ct >= 86400 && ct < 2592000) { //86400 * 30
            val day = ct / 86400
            return day.toString() + "天前"
        }
        return if (ct >= 2592000 && ct < 31104000) { //86400 * 30
            "${ct / 2592000}月前"
        } else "${ct / 31104000}年前"
    }

    /**
     * 转化为剩余时间
     */
    fun getLeftTime(distance: Long): String {
        var distance = distance
        if (distance >= 31104000) {
            distance = distance / 31104000
            result1 += distance.toString() + "年"
        } else if (distance >= 2592000) {
            distance = distance / 2592000
            result1 += distance.toString() + "月"
        } else if (distance >= 86400) {
            distance = distance / 86400
            result1 += distance.toString() + "天"
        } else if (distance >= 3600) {
            distance = distance / 3600
            result1 += distance.toString() + "小时"
        } else if (distance >= 60) {
            distance = distance / 60
            result1 += distance.toString() + "分"
        } else if (distance >= 1) {
            result1 += distance.toString() + "秒"
        } else {
            return result1
        }
        getLeftTime(distance)
        return result1
    }

    /**
     * 将时间字符串转位日期类型
     *
     * @param sdate
     * @return
     */
    fun toDate(sdate: String?): Date? {
        return try {
            dateFormater.get().parse(sdate)
        } catch (e: ParseException) {
            null
        }
    }

    /**
     * 将yyyy-MM-dd"字符串转为秒级时间戳
     *
     * @param
     * @return
     */
    @JvmOverloads
    fun date2TimeStamp(date_str: String?, format: String? = "yyyy-MM-dd"): String {
        try {
            val sdf = SimpleDateFormat(format)
            return (sdf.parse(date_str).time / 1000).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

    fun date2TimeStampLong(date_str: String?, format: String?, ismill: Boolean): Long {
        try {
            val sdf = SimpleDateFormat(format)
            return if (ismill) {
                //毫秒级
                sdf.parse(date_str).time
            } else {
                //秒级
                sdf.parse(date_str).time
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    fun timeStamp2Date(seconds: String?): String {
        if (seconds == null || seconds.isEmpty() || "null" == seconds) {
            return ""
        }
        val format = "yyyy-MM-dd"
        val sdf = SimpleDateFormat(format)
        return sdf.format(Date(java.lang.Long.valueOf(seconds + "000")))
    }

    fun getTime(time: Int): String {

        return if (time / 60 < 60) {
            //正常时间没有小时
            "${
                if (time / 60 < 10) {
                    "0${time / 60}"
                } else {
                    time / 60
                }
            }:${
                if (time % 60 < 10) {
                    "0${time % 60}"
                } else {
                    time % 60
                }
            }"
        } else {
            "${
                if (time / 60 / 60 < 10) {
                    "0${time / 60 / 60}"
                } else {
                    time / 60 / 60
                }
            }:${
                if (time / 60 % 60 < 10) {
                    "0${time / 60 % 60}"
                } else {
                    time / 60 % 60
                }
            }:${
                if (time % 60 < 10) {
                    "0${time % 60}"
                } else {
                    time % 60
                }
            }"
        }
    }

    fun timeMSStringToInt(time: String): Int {
        val list = time.split(":")
        return list[0].toInt() * 60 + list[1].toInt()
    }
}
